import socket
import select


s = socket.socket()

# host = socket.gethostname()
# 直接用IP
host = '127.0.0.1'

port = 1234
s.bind((host, port))
s.listen(5)
inputs = [s]

while True:
    rs, ws, es = select.select(inputs, [], [])
    for r in rs:
        if r is s:
            c, addr = s.accept()
            print('Got connection from', addr)
            inputs.append(c)
        else:
            data = False
            try:
                data = r.recv(1024)
                disconnect = not data
            except socket.error:
                disconnect = True

            if disconnect:
                print(r.getpeername(), 'disconnected')
                inputs.remove(r)
            else:
                print(data)
